<?php
header("Content-Type:json/html;charset=utf8;");

$config = require '../config.php';
$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['dbname']);

/**
 * @param mysqli $conn
 * @param string $sql
 * @return array
 */
function getMysqli_result(mysqli $conn, string $sql): array
{
    $result = $conn->query($sql);
    $conn->close();

    $data = array();
    if ($result->num_rows >= 1) {
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
        $result = ['code' => 1, 'data' => array(
            'article' => $data[0]['article_total'],
            'comment' => $data[1]['article_total'],
            'users' => $data[2]['article_total'],
        ), 'msg' => "获取数据成功!"];
    } else {
        $result = ['code' => 0, 'msg' => "获取数据失败!"];
    }
    return $result;
}

    $sql = "select COUNT(*) as article_total from article union  select COUNT(*) from comment union  select COUNT(*)from comment union all select count(*) from users;";
    $result = getMysqli_result($conn, $sql);

print_r(json_encode($result));

